import type { RouteRecordRaw } from 'vue-router'
import { localCache } from './cache'
import router from '@/router'

function loadLocalRoutes(): RouteRecordRaw[] {
  // 动态获取所有的路由对象，放到数组中
  // 路由对象都在独立的文件中
  // 从文件中将所有路由对象先读到数组中
  const localRoutes: RouteRecordRaw[] = []
  // 读取router/main所有的ts文件
  const files: Record<string, any> = import.meta.glob('../router/main/**/*.ts', { eager: true })
  // console.log(files)
  for (const key in files) {
    const module = files[key]
    localRoutes.push(module.default)
  }
  // console.log(localRoutes)
  return localRoutes
}

export let firstMenu: any = null

export function mapMenusToRotes(userMenus: any[]): RouteRecordRaw[] {
  const localRoutes = loadLocalRoutes()
  const routeItems: RouteRecordRaw[] = []
  // 根据菜单去匹配正确的路由
  for (const menu of userMenus) {
    for (const subMenu of menu.children) {
      const routeItem = localRoutes.find((item) => item.path === subMenu.url)
      if (routeItem) {
        // 给route的顶层菜单增加重定向功能(因为我只需把该一级菜单的url重定向第一个二级菜单的url即可,所以只需添加一次)
        if (!routeItems.find((item) => item.path === menu.url)) {
          routeItems.push({ path: menu.url, redirect: routeItem.path })
        }

        // 将二级菜单对应的路径添加进去
        routeItems.push(routeItem)
      }
      // 记录第一个被匹配到的菜单
      if (!firstMenu && routeItem) {
        firstMenu = subMenu
      }
    }
  }
  return routeItems
}

export function deleteMenusRotes() {
  const routeItems = mapMenusToRotes(localCache.getCache('userMenus'))
  routeItems.forEach((item) => {
    if (item.name) {
      router.removeRoute(item.name)
    }
  })
}

export function mapPathToMenu(path: string, userMenus: any[]) {
  for (const menu of userMenus) {
    for (const subMenu of menu.children) {
      if (subMenu.url === path) {
        return subMenu
      }
    }
  }
  return undefined
}

interface IBreadcrumbs {
  name: string
  path: string
}

export function mapPathToBreadcrumbs(path: string, userMenus: any[]) {
  // 定义面包屑数组
  const breadcrumbs: IBreadcrumbs[] = []
  // 遍历获取面包屑层级
  for (const menu of userMenus) {
    for (const submenu of menu.children) {
      if (submenu.url == path) {
        // 顶级菜单
        breadcrumbs.push({ name: menu.name, path: menu.url })
        // 匹配菜单
        breadcrumbs.push({ name: submenu.name, path: submenu.url })
      }
    }
  }
  // console.log(breadcrumbs)
  return breadcrumbs
}

// 菜单映射到id列表
export function mapMenuListToIds(menuList: any[]) {
  const ids: number[] = []

  function recurseGetId(menus: any[]) {
    for (const item of menus) {
      if (item.children) {
        recurseGetId(item.children)
      } else {
        ids.push(item.id)
      }
    }
  }

  recurseGetId(menuList)

  return ids
}

// 从菜单映射到按钮的权限
export function mapMenusToPermissions(menuList: any[]) {
  const permission: string[] = []
  function recurseGetPermission(menus: any[]) {
    for (const item of menus) {
      if (item.type === 3) {
        permission.push(item.permission)
      } else {
        recurseGetPermission(item.children ?? [])
      }
    }
  }

  recurseGetPermission(menuList)

  return permission
}
